<%@page import="cn.egame.interfaces.gc.GameInfo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="cn.egame.common.model.PageData"%>
<%@page import="java.util.*"%>
<%@page import="cn.egame.interfaces.pu.AppParameter"%>
<%@page import="cn.egame.interfaces.gc.GameInfo"%>  
<%@page import="cn.egame.interfaces.gc.GameTagLinkInfo"%> 
<%@page import="com.egame.manage.biz.server.pu.ParameterServiceImpl"%>
<%@page import="com.egame.manage.biz.utils.ConstVar"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.4.2.min.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/main.css"  />
<title>推荐列表</title>
</head>
<body>
<script type="text/javascript">
	$(document).ready(function() {
		$("#appSortList").click(function(){
			var sortId = $("#appSortList").val();
			$("#sortSelect").attr("value",sortId);
		});
		var sortId = $("#sortSelect").attr("value");
		var target = "#appSortList option[value='" + sortId + "']";
		$(target).attr("selected","selected");
	});

	function getOptionValueList(selectId){
		var options = document.getElementById(selectId);
		var result = new Array();
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				result.push(options[i].value);
			}
		}
		return result;
	}
	

	function getOptionValue(selectName){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		return options[optionIndex].value;
	}
	
	function getOptionText(selectName){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		return options[optionIndex].text;
	}

	
	function setOptionText(selectName,text){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		options[optionIndex].text = text;
	}

	
	function removeOption(selectName){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		if(options == null)return null;
		var optionIndex = select.selectedIndex;
		if(optionIndex == null)return null;
		options.remove(optionIndex);
	}
	
	

	function addOption(selectName,value,text){
		var select = document.getElementById(selectName);
		if(select == null)return null;
		var options = select.options;
		options.add(new Option(text,value));		
		options[options.length-1].selected = "selected";
	}
	
	function moveOption(fromId,toId){
		var options = document.getElementById(fromId);
		var toOptions = document.getElementById(toId);
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				toOptions.add(new Option(options[i].text,options[i].value));
				options.remove(i);
				--i;
			}
		}
	}

	function moveToOption(fromId,toId){
		var options = document.getElementById(fromId);
		var toOptions = document.getElementById(toId);
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				toOptions.options.add(new Option(options[i].text,options[i].value),0);
				options.remove(i);
				--i;
			}
		}
	}
	
	function addRecommendGame(url){
		var value = getOptionValueList("gameList");
		if(value==null || value==""){
			alert("请选择要添加的游戏");
			return;
		}
		var sortSelect = $("#appSortList").val();
		var sortSel = $("#sortSelect").attr("value");
		
		if(sortSelect==""||sortSelect==null){
			alert("请选择分类");
			return;
		}else if(sortSelect.length>1){
			alert("只能选择一种分类");
			return;
		}else{
			document.getElementById("gid").value = value;
			document.getElementById("myform").action = '<%=request.getContextPath() %>' + url +"?select="+sortSel;
			document.getElementById("myform").submit();
		}
	}
	
	function deleteRecommendGame(url){
		var value = getOptionValueList("recommendList");
		if(value==null || value==""){
			alert("请选择要移除的游戏");
			return;
		}
		var sortSel = $("#sortSelect").attr("value");
		document.getElementById("gid").value = value;
		document.getElementById("myform").action = '<%=request.getContextPath() %>'  + url +"?select="+sortSel;
		document.getElementById("myform").submit();
	}
	
	function moveRecommendGame(url){
		var value = getOptionValueList("recommendList");		
		if(value==null || value==""){
			alert("请选择要移动的游戏");
			return;
		}else{
			var values = value.toString().split(",");
			if(values.length>1){
				alert("只能选择一个要移动的游戏");
				return;
			}			
		}
		var sortSel = $("#sortSelect").attr("value");
		document.getElementById("gid").value = value;
		document.getElementById("myform").action = '<%=request.getContextPath() %>' + url +"?select="+sortSel;
		document.getElementById("myform").submit();
	}	
	//

	function getRecommendList(){		
		var myform = document.getElementById("myform");
		myform.action = myform.action + getOptionValue("position");
		myform.submit();		
	}
	
	function checkValue(position,recommend){
		positionValue = getOptionValue(position);		
		recommendValue = getOptionValueList(position);
		
		if(positionValue == null || positionValue == ""){
			alert("请选择推荐位置");
			return;
		}
		if(positionValue == null || positionValue == ""){
			alert("请选择推荐游戏");
			return;
		}
	}

	function getFlagList(selectId,flag){
		var options = document.getElementById(selectId);
		var result = new Array();
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				result.push(options[i].text.indexOf(flag)>=0?0:1);
			}
		}
		return result;
	}

	function setFlagList(selectId,flag){
		var options = document.getElementById(selectId);
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				if(options[i].text.indexOf(flag)>=0){
					options[i].text = options[i].text.split(flag)[1];
				}else{
					options[i].text = flag + options[i].text;
				}				
			}
		}
	}
	
	
	

	function upper() {var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要上移的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/upper.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index()>0){
				optionIndex.insertBefore(optionIndex.prev());
			}
		}
	});		
}

function lower() {
	var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要下移的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/lower.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index() < (recommendSelect.find("option").length-1)){
				optionIndex.insertAfter(optionIndex.next());
			}
		}
	});		
	
}

function toTop() {
	var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要置顶的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/top.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index()>0){
				optionIndex.insertBefore(recommendSelect.find("option").first());
			}
		}
	});		
}

function toDown(){
	var value = getOptionValueList("recommendList");
	if(value==null || value==""){
		alert("请选择要置底的游戏");
		return;
	}
	var values = value.toString().split(",");
	if(values.length>1){
		alert("只能选择一个要移动的游戏");
	}
	$.ajax({			
		url : "<%=request.getContextPath() + "/tv/tag/down.do" %>",
		type : 'post',
		data : {
			'gid' : getOptionValueList("recommendList").toString(),
			'tagid' : $('#position').val()
		},
		success : function(msg) {		
			var recommendSelect = $("#recommendList");
			var optionIndex = recommendSelect.find("option:selected");
			if(optionIndex.index() < (recommendSelect.find("option").length-1)){
				optionIndex.insertAfter(recommendSelect.find("option").last());
			}
		}
	});	
	
}
	
	
	
	function editAppSort() {
		var sort=$("#appSortList").val();
		domain="<%=request.getContextPath() + "/store/game/sort/edit/show.do?id="%>"+sort;
		
		if(sort == null || sort == ""){
			alert("请选择分类");
			return;
		}else{
			window.location.href=domain;
		}
	}
	
	function upAppSort() {
		var sort=$("#appSortList").val();
		domain="<%=request.getContextPath() + "/store/game/sort/upper.do?id="%>"+sort;
		
		if(sort == null || sort == ""){
			alert("请选择分类");
			return;
		}else{
			window.location.href=domain;
		}
	}

	function downAppSort() {
		var sort=$("#appSortList").val();
		domain="<%=request.getContextPath() + "/store/game/sort/lower.do?id="%>"+sort;
		
		if(sort == null || sort == ""){
			alert("请选择分类");
			return;
		}else{
			window.location.href=domain;
		}
	}
	
	function deleteAppSort() {
		recommendValue = getOptionValue("appSortList");
		recommendText = getOptionText("appSortList");
		if(recommendValue == null || recommendValue == ""){
			alert("请选择分类");
			return;
		}
		$.ajax({			
			url : "<%=request.getContextPath() + "/store/game/sort/delete.do" %>",
			type : 'post',
			data : {
				'gid' : getOptionValueList("appSortList").toString()
			},
			success : function(msg) {	
				removeOption("appSortList");
			}
		});	
	}
	
</script>
<form id="myform" action="<%=request.getContextPath() + "/store/game/sort/list.do" %>" method="post">
<input type="hidden" name=gid id=gid>
<% 	
	String gameName = "";
	if(request.getAttribute("gameName")!=null){
		gameName = (String)request.getAttribute("gameName");
	}
	String recommendName = "";
	if(request.getAttribute("recommendName")!=null){
		recommendName = (String)request.getAttribute("recommendName");
	}	

	int sortId = 0;
	if(request.getAttribute("sortId")!=null){
		sortId = (Integer)request.getAttribute("sortId");
	}
%>
	<div class="matter_box">
		<h3 class="nopoint"><strong>彩蛋>游戏>分类</strong></h3>
		<div class="mt10 pt10 clearfix">
<table>
	<tr>
		<td>
			<table>				
				<tr>
					<td width=35% align=center><p style="font-size: 16px;margin-bottom: 20px;font-weight: bold;">分类列表:</p></td>
					<td></td>
					<td width=30% align=center><p style="font-size: 16px;margin-bottom: 20px;font-weight: bold;">内容列表:</p></td>
					<td></td>					
				</tr>	
				<tr>
					<td colspan=2>
					</td>
					<td colspan=2>
						<input type="hidden" id="sortSelect" name="sortSelect" value="<%=sortId %>">
						游戏ID/游戏名称:<input type=text name="recommendname"  value="<%=recommendName %>">
						<input type="submit" value="查询" class="btn3">
					</td>					
				</tr>			
				<tr>
					<td>
						<select name="appSortList" id="appSortList" size="10" style="width:180px;margin-left: 50px;" multiple="multiple">
							<%
								if(request.getAttribute("appSortList")!=null){
									List<AppParameter> list = (List<AppParameter>)request.getAttribute("appSortList");
									for(AppParameter bean:list){
										%>
											<option id="<%=bean.getId() %>" value=<%=bean.getId() %>><%=bean.getName() %>												
											</option>
										<%
									}
								}
								
							%>
						</select>
					</td>
					<td>
						<a class="btn3" href="<%=request.getContextPath() + "/store/game/sort/set/show.do"%>">新增</a> <br/>
						<input type=button value="编辑" class="btn3"  onclick="editAppSort()"> <br/>
						<input type=button value="上移" class="btn3"  onclick="upAppSort()"> <br/>
						<input type=button value="下移" class="btn3"  onclick="downAppSort()"> <br/>
						<input type=button value="删除" class="btn3"  onclick="deleteAppSort()"> <br/>
						
					</td>
					
					<td>
						<select name="recommendList" id="recommendList" size="20" style="width:240px;"  multiple="multiple">
							<%
								if(request.getAttribute("recommendList")!=null){
									List<GameInfo> list = (List<GameInfo>)request.getAttribute("recommendList");
									for(GameInfo bean:list){
										%>
											<option id="<%=bean.getGId() %>" value=<%=bean.getGId() %>>												
												[<%=bean.getGId() %>]--
												<%=bean.getGameName() %>--
												(<%=ParameterServiceImpl.getInstance().getParameterNameById(0,0L,ConstVar.APP_OS,""+bean.getOsCode()) %>)
											</option>
										<%
									}
								}
								
							%>
						</select>
					</td>
					<td>
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/sort/upper.do')" value="上移"><p>
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/sort/lower.do')" value="下移"><p>
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/sort/top.do')" value="置顶"><p>
						<input type="button" class="btn3" onclick="moveRecommendGame('/store/game/recommend/sort/down.do')" value="置底"><p>
					</td>					
				</tr>
				
				<!-- 下一大行 -->
				<tr>
					<td></td>
					<td></td>
				</tr>	
				<tr>
					<td colspan=2>
						游戏ID/游戏名称:<input type=text name="gname" value="<%=gameName %>">
						<input type=submit value="查询" class="btn3">
					</td>
				</tr>			
				<tr>
					<td>
						<select name="gameList" id="gameList" size="20" style="width:240px;" multiple="multiple">
							<%
								if(request.getAttribute("gameList")!=null){
									List<GameInfo> list = (List<GameInfo>)request.getAttribute("gameList");
									for(GameInfo bean:list){
										%>
											<option id="<%=bean.getGId() %>" value=<%=bean.getGId() %>>												
												[<%=bean.getGId() %>]--
												<%=bean.getGameName() %>--
												(<%=bean.getOsCode() %>)
											</option>
										<%
									}
								}
								
							%>
						</select>
						
					</td>
					<td>
						<input type=button value="添加 >" class="btn3"  onclick="addRecommendGame('/store/game/recommend/sort/add.do')"> <br/>
						<input type="button" class="btn3" onclick="deleteRecommendGame('/store/game/recommend/sort/delete.do')" value="< 移除"><br/>
					</td>
					
				</tr>
			</table>
		</td>
	</tr>
</table>
</div>
</div>
</form>
</body>
</html>